package com.ys.everydaynews.ui.main.activityy

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseActivity
import com.ys.everydaynews.base.Constants
import com.ys.everydaynews.databinding.LayoutLoginNopassBinding
import com.ys.everydaynews.ui.main.presenter.RegisterPresenter
import com.ys.everydaynews.ui.main.view.RegisterView
import com.ys.everydaynews.utils.CommonUtils
import com.ys.everydaynews.utils.toast
import com.ys.everydaynews.view.MyCheckBox

class LoginNoPassActivity :
    BaseActivity<RegisterView, RegisterPresenter, LayoutLoginNopassBinding>(), RegisterView,
    TextWatcher {
    private var phoneNumber: String = ""

    override fun getLayoutId(): Int = R.layout.layout_login_nopass

    override fun initView() {
        binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        binding.btnBack.setOnClickListener { finish() }
        binding.edtMoblie.addTextChangedListener(this)
        binding.checkbox.initSpan(R.string.mmxy)
        binding.checkbox.isShow(false)
        binding.tvLogin.setOnClickListener { startActivity(intent.setClass(this,LoginPassActivity::class.java)) }
        binding.tvRegister.setOnClickListener { startActivity(intent.setClass(this,RegisterAtivity::class.java)) }
        binding.tvSet.setOnClickListener { startActivity(intent.setClass(this,PrivacySetActivity::class.java)) }
        binding.checkbox.setClickableSpanText(object : MyCheckBox.ClickableSpanText {
            override fun onClick(type: Int) {
                if (type == 1) {
                    "用户协议".toast(this@LoginNoPassActivity)
                } else {
                    "隐私政策".toast(this@LoginNoPassActivity)
                }
            }
        })
        binding.cdNext.setOnClickListener {
            if (TextUtils.isEmpty(phoneNumber)) {
                Toast.makeText(this@LoginNoPassActivity, "请填写正确电话", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            //TODO 验证码跳转
            val intent= Intent(this,SendCodeActivity::class.java)
            intent.putExtra(Constants.MOBLIE,phoneNumber)
            startActivity(intent)
        }
    }

    override fun initData() {
    }

    override fun createPersenter(): RegisterPresenter = RegisterPresenter()

    override fun isRegister(): Boolean = false
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phoneNumber = s.toString()
        checkPhone()
    }

    override fun afterTextChanged(s: Editable?) {

    }
    private fun checkPhone() {
        if (phoneNumber.length == 13  && CommonUtils.checkPhoneNum(
                CommonUtils.replaceBlank(phoneNumber)
            )
        ) {
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_red))
        } else {
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        }
    }
}